//
//  CAnimationSystem.cpp
//  Monster_Martial_Arts_Version_0
//
//  Created by chiawei.tseng on 13/1/7.
//
//

#include "CAnimationSystem.h"
#include "CBattleObject.h"


CAnimationSystem::CAnimationSystem()
{
    m_MyMaster = NULL;
}

CAnimationSystem::~CAnimationSystem()
{
    
}

void CAnimationSystem::Init(CBattleObject* pMyMaster)
{
    m_MyMaster = pMyMaster;
    
    m_nPreActiveAnaimationID = INVALID_ACTIVE_ANIMATION_ID;
    
    m_nCurrActiveAnimationID = INVALID_ACTIVE_ANIMATION_ID;
    
    m_nNextActiveAnimationID = INVALID_ACTIVE_ANIMATION_ID;
    
    LoadAnimation();
}

void CAnimationSystem::Update(float dt)
{
    if (m_nCurrActiveAnimationID != m_nNextActiveAnimationID && m_nNextActiveAnimationID != INVALID_ACTIVE_ANIMATION_ID)
    {
        
        bool isChangeAA = false;
        
        if (m_nCurrActiveAnimationID != INVALID_ACTIVE_ANIMATION_ID)
        {
            isChangeAA = m_mapAnaimations[m_nCurrActiveAnimationID]->isCanChangeActiveAnimation();
        }
        else
        {
            isChangeAA = true;
        }
        
        
        if(isChangeAA)
        {
            if (m_nCurrActiveAnimationID != INVALID_ACTIVE_ANIMATION_ID)
                m_mapAnaimations[m_nCurrActiveAnimationID]->LeavingAnimation();
            
            m_nCurrActiveAnimationID = m_nNextActiveAnimationID;
            
            m_mapAnaimations[m_nCurrActiveAnimationID]->EnteringAnimation();
            
            m_nNextActiveAnimationID = INVALID_ACTIVE_ANIMATION_ID;
        }
    }
    
    if(m_nCurrActiveAnimationID != INVALID_ACTIVE_ANIMATION_ID)
    {
        m_mapAnaimations[m_nCurrActiveAnimationID]->RunAnimation(dt);
    }
}

void CAnimationSystem::Release()
{
    map<int,CActiveAnimation*>::iterator pi = m_mapAnaimations.begin();
    
    while ( pi != m_mapAnaimations.end())
    {
        pi->second->Release();
        
        delete pi->second;
        
        pi->second = NULL;
        
        pi++;
    }
    
    m_mapAnaimations.clear();
}

void CAnimationSystem::ChangeActiveAnimation(ACTIVE_ANIMATION_ID ActiveAnimationID)
{
    if (m_nCurrActiveAnimationID == ActiveAnimationID && m_nNextActiveAnimationID != INVALID_ACTIVE_ANIMATION_ID)
        return ;
    
    {
        if (m_nNextActiveAnimationID == INVALID_ACTIVE_ANIMATION_ID)
        {
            m_nNextActiveAnimationID = ActiveAnimationID;
        }
        
        
    }
    
}

ACTIVE_ANIMATION_ID CAnimationSystem::GetCurrActiveAnimationID()
{
    return static_cast<ACTIVE_ANIMATION_ID>(m_nCurrActiveAnimationID);
}

void CAnimationSystem::AddCallBackFunction(ACTIVE_ANIMATION_ID AAnimationID,CCallBack* Ptr)
{
    m_mapAnaimations[AAnimationID]->RegisterCallBack(Ptr);
}


void CAnimationSystem::PlayeToMyMasterAnimation(const char* Name)
{
    m_MyMaster->PlayToAnimation(Name);
}

void CAnimationSystem::UpdateMyMasterAnimation(float dt)
{
    m_MyMaster->UpdateAnimation(dt);
}

void CAnimationSystem::RegisterAniEventListener(CCAnimationEventListener* pListener)
{
    m_MyMaster->RegisterAniEventListen(pListener);
}

void CAnimationSystem::LoadAnimation()
{
    /*
    struct AnimationData
    {
        ACTIVE_ANIMATION_ID m_nActiveAnimationID;
        ACTIVE_ANIMATION_ID m_nNextActiveAnimationID;
        ANIMATION_ID m_AniamtionIDs[ANIMATION_EVENT_MAX];
        int  m_AnimationCount;
        bool m_RunLoop;
    };
    */
    
    /*
     enum ACTIVE_ANIMATION_ID
     {
     INVALID_ACTIVE_ANIMATION_ID =-1,
     ACTIVE_ANIMATION_ID_IDLE,
     ACTIVE_ANIMATION_ID_WORK,
     ACTIVE_ANIMATION_ID_ATTACK,
     ACTIVE_ANIMATION_ID_LONG_ATTACK,
     ACTIVE_ANIMATION_ID_HEAL,
     ACTIVE_ANIMATION_ID_HURT,
     ACTIVE_ANIMATION_ID_DEATH,
     ACTIVE_ANIMATION_ID_USE_SKILLS_1,
     ACTIVE_ANIMATION_ID_USE_SKILLS_2,
     ACTIVE_ANIMATION_ID_USE_SKILLS_3,
     
     
     ACTIVE_ANIMATION_ID_MAX
     };
     
     enum ANIMATION_ID
     {
     INVALID_ANIMATION_ID = -1,
     ANIMATION_ID_STAND,
     ANIMATION_ID_RUN,
     ANIMATION_ID_ATTACK,
     ANIMATION_ID_HEAL,
     
     ANIMATION_ID_MAX
     };
     */
    
    {
        CActiveAnimation::AnimationData tmp;
        
        tmp.m_nActiveAnimationID = ACTIVE_ANIMATION_ID_IDLE;
        tmp.m_nNextActiveAnimationID = INVALID_ACTIVE_ANIMATION_ID;
        
        tmp.m_AniamtionIDs[0] = ANIMATION_ID_STAND;
        tmp.m_AniamtionIDs[1] = INVALID_ANIMATION_ID;
        tmp.m_AniamtionIDs[2] = INVALID_ANIMATION_ID;
        
        tmp.m_AnimationCount = 1;
        
        tmp.m_RunLoop = true;
        
        CActiveAnimation* AAnimation = new CActiveAnimation();
        
        AAnimation->Init(this,tmp);
        
        m_mapAnaimations[ACTIVE_ANIMATION_ID_IDLE] = AAnimation;
    }
    
    {
        CActiveAnimation::AnimationData tmp;
        
        tmp.m_nActiveAnimationID = ACTIVE_ANIMATION_ID_MOVE;
        tmp.m_nNextActiveAnimationID = INVALID_ACTIVE_ANIMATION_ID;
        
        tmp.m_AniamtionIDs[0] = ANIMATION_ID_RUN;
        tmp.m_AniamtionIDs[1] = INVALID_ANIMATION_ID;
        tmp.m_AniamtionIDs[2] = INVALID_ANIMATION_ID;
        
        tmp.m_AnimationCount = 1;
        
        tmp.m_RunLoop = true;
        
        CActiveAnimation* AAnimation = new CActiveAnimation();
        
        AAnimation->Init(this,tmp);
        
        m_mapAnaimations[ACTIVE_ANIMATION_ID_MOVE] = AAnimation;
    }
    
    {
        CActiveAnimation::AnimationData tmp;
        
        tmp.m_nActiveAnimationID = ACTIVE_ANIMATION_ID_ATTACK;
        tmp.m_nNextActiveAnimationID = ACTIVE_ANIMATION_ID_IDLE;
        
        tmp.m_AniamtionIDs[0] = ANIMATION_ID_ATTACK;
        tmp.m_AniamtionIDs[1] = INVALID_ANIMATION_ID;
        tmp.m_AniamtionIDs[2] = INVALID_ANIMATION_ID;
        
        tmp.m_AnimationCount = 1;
        
        tmp.m_RunLoop = false;
        
        CActiveAnimation* AAnimation = new CActiveAnimation();
        
        AAnimation->Init(this,tmp);
        
        m_mapAnaimations[ACTIVE_ANIMATION_ID_ATTACK] = AAnimation;
    }
    
}







